<template>
  <div class="app-container">
    <Ht-search-form
      v-show="showSearch"
      :dictMap="dictMapAsync"
      :queryParams="queryParams"
      :outputQueryParams="queryParams"
      :formOptions="formOptions"
      :advOptions="[]"
      :defaultEventKey="defaultEventKey"
      :labelWidth="80"
    ></Ht-search-form>

    <!--    <div class="page-label-block">-->
    <!--      <span class="color-block">|</span>w-->
    <!--      <span class="page-label-name">-->
    <!--      租户管理-->
    <!--      </span>-->
    <!--    </div>-->

    <ht-table
      ref="dataTable"
      :multi-select="false"
      :showSerialNumber="false"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :tableBindColumns="tableBindColumns"
      :reqConfig="regConfig"
      :defaultEventKey="defaultEventKey"
      :randomRowKey="true"
    >
      <template v-slot:operate>
        <el-table-column label="操作" align="left" width="50" fixed="right">
          <template slot-scope="scope">
            <ht-button-group
              :buttonList="[
                {
                  label: '明细',
                  doFunc: () => {
                    handleAction('detail', scope.row);
                  },
                },
              ]"
            />
          </template>
        </el-table-column>
      </template>
    </ht-table>

    <!-- 添加或修改弹出框 -->
    <el-dialog :title="title" center :visible.sync="open" width="1024px" append-to-body :close-on-click-modal="false">
      <el-form ref="detailForm" :model="detailForm" :rules="rules" label-width="120px">
        <el-card class="form-card txnInfo-card">
          <div slot="header" class="label-header-msg">
            <span>实时客户支付协议管理明细信息</span>
          </div>

          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="交易流水号" prop="id">
                <el-input v-model="detailForm.id" :disabled="true" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="报文标识号" prop="pkgId">
                <el-input v-model="detailForm.pkgId" :disabled="true" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="工作日期" prop="workdate">
                <el-date-picker
                  v-model="detailForm.workdate"
                  style="width: 100%"
                  type="date"
                  value-format="yyyyMMdd"
                  :disabled="true"
                />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="扣款管理类型" prop="dbtMngType">
                <el-select
                  style="width: 100%"
                  v-model="detailForm.dbtMngType"
                  placeholder="扣款管理类型"
                  :disabled="true"
                >
                  <el-option
                    v-for="dict in dictMap.DebitManagementTypeCode"
                    :key="dict.value"
                    :label="`${dict.value}-${dict.text}`"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="扣款通知编号" prop="dbtNo">
                <el-input v-model="detailForm.dbtNo" :disabled="true" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="地域标识" prop="regionId">
                <el-select style="width: 100%" v-model="detailForm.regionId" placeholder="地域标识" :disabled="true">
                  <el-option
                    v-for="dict in dictMap.RegionId"
                    :key="dict.value"
                    :label="`${dict.value}-${dict.text}`"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="统一社会信用代码" prop="uniformScCode">
                <el-input v-model="detailForm.uniformScCode" :disabled="true" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="付款人数目" prop="payerNum">
                <el-input v-model="detailForm.payerNum" :disabled="true" />
              </el-form-item>
            </el-col>
          </el-row>

          <div class="discountReceipt-pick-operate-box">
            <div class="operate-box-bottom">
              <div class="bottom-content">
                <ht-table
                  ref="dataTableDetail"
                  :multi-select="false"
                  :showSerialNumber="false"
                  :dictMap="dictMap"
                  :queryParams="queryParamsDetail"
                  :tableBindColumns="tableBindColumnsDetail"
                  :reqConfig="regConfigDetail"
                  :showRightToolbar="false"
                  :randomRowKey="true"
                />
              </div>
            </div>
          </div>
        </el-card>

        <el-card class="form-card txnInfo-card">
          <div slot="header" class="label-header-msg">
            <span>客户账户扣款管理应答明细信息</span>
          </div>

          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="发起参与机构号" prop="sender">
                <el-input v-model="userInfo.cnapsBrno" :disabled="true" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发起参与机构名称" prop="senderName">
                <el-input v-model="userInfo.cnapsBrname" :disabled="true" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="接收参与机构号" prop="recver">
                <el-input v-model="detailForm.recver" :disabled="true" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="接收参与机构名称" prop="recverName">
                <el-input v-model="detailForm.recverName" :disabled="true" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="工作日期" prop="workdate372">
                <el-date-picker
                  v-model="detailForm.workdate372"
                  style="width: 100%"
                  type="date"
                  value-format="yyyyMMdd"
                />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="业务状态" prop="bizStatus">
                <el-select style="width: 100%" v-model="detailForm.bizStatus" placeholder="业务状态">
                  <el-option
                    v-for="dict in dictMap.PROCESSSTATUS"
                    :key="dict.value"
                    :label="`${dict.value}-${dict.text}`"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <div v-if="this.detailForm.bizStatus === 'PR09'">
            <el-row type="flex">
              <el-col :span="8">
                <el-form-item label="业务拒绝处理码" prop="bizRejectCode">
                  <el-input
                    v-model="detailForm.bizRejectCode"
                    placeholder="请输入业务拒绝处理码"
                    @change="getbase('REJECTCODE')"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-input v-model="detailForm.bizRejectCodeDesc" :disabled="true" />
              </el-col>
              <el-col :span="4">
                <el-row>
                  <el-button type="primary" icon="el-icon-search" plain @click="openBasedataDialog('REJECTCODE')">
                    请选择
                  </el-button>
                </el-row>
              </el-col>
            </el-row>

            <el-row type="flex">
              <el-col :span="24">
                <el-form-item label="业务拒绝信息" prop="bizRejectInfo">
                  <el-input type="textarea" v-model="detailForm.bizRejectInfo" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>

          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="业务处理参与机构" prop="bizProOrg">
                <el-input v-model="detailForm.bizProOrg" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="原扣款通知编号" prop="orgDbtNo">
                <el-input v-model="detailForm.orgDbtNo" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex">
            <el-col :span="12">
              <el-form-item label="付款人数目" prop="payerNum372">
                <el-input v-model="detailForm.payerNum372" :disabled="true" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex" v-if="this.detailForm.bizStatus !== 'PR09'">
            <ht-table
              ref="dataTableDbt"
              :multi-select="false"
              :showSerialNumber="false"
              :dictMap="dictMap"
              :tableBindColumns="tableBindColumnsDbt"
              :opButtonList="opButtonList"
              :showRightToolbar="false"
              :showPagination="false"
              :randomRowKey="true"
            >
              <template v-slot:operate>
                <el-table-column label="操作" align="left" width="100" fixed="right">
                  <template slot-scope="scope">
                    <ht-button-group
                      :buttonList="[
                        {
                          label: '维护',
                          doFunc: () => {
                            editRow(scope.$index, scope.row);
                          },
                        },
                        {
                          label: '删除',
                          doFunc: () => {
                            deleteRow(scope.$index, scope.row);
                          },
                        },
                      ]"
                    />
                  </template>
                </el-table-column>
              </template>
            </ht-table>
          </el-row>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="save" v-loading.fullscreen.lock="loading">提 交</el-button>
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :title="titleDtb"
      :visible.sync="openDtb"
      center
      v-if="openDtb"
      append-to-body
      width="1024px"
    >
      <el-form ref="detailFormDtb" :model="detailFormDtb" :rules="rulesDtb" label-width="140px">
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="扣款明细标识号" prop="dbtDtlId">
              <el-input v-model="detailFormDtb.dbtDtlId" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="扣款结果业务状态" prop="dbtStatus">
              <el-select style="width: 100%" v-model="detailFormDtb.dbtStatus" placeholder="扣款结果业务状态">
                <el-option
                  v-for="dict in dictMap.CHECK_PROC_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" align="center" class="dialog-footer">
        <el-button type="primary" @click="detailSave(detailForm.index)">保 存</el-button>
        <el-button type="primary" @click="cancelDtb">关 闭</el-button>
      </div>
    </el-dialog>
    <base-data-dialog ref="basedataDialog" @confirm="confirmData"></base-data-dialog>
  </div>
</template>

<script>
import { listPageMixin } from "@/libs/listPageMixin";
import {
  apiUrlConfigEntry,
  apiUrlConfigEntryDetailTabSearch,
  custAccDeductionAnswerEntryDetail,
  custAccDeductionAnswerEntrySave,
} from "@/api/cnaps/beps/372";
import { amountThousandsFormatter } from "@/utils/table_col_formatter";
import { getBasedata } from "@/api/cnaps/common/basedata";
import BaseDataDialog from "@/views/components/BasedataDialog";

export default {
  name: "CustAccDeductionAnswerEntry",
  mixins: [listPageMixin],
  components: { BaseDataDialog },
  data() {
    return {
      // 查询参数
      queryParams: {
        id: undefined,
        workdate: undefined,
        workdate2: undefined,
      },
      queryParamsDetail: {
        id: undefined,
      },
      loading: false,
      // 搜索
      defaultEventKey: "defaultEventKey",
      formOptions: [],
      //
      searchFormDictList: ["DebitManagementTypeCode", "RegionId", "PROCESSSTATUS", "CHECK_PROC_STATUS"],
      // 列信息
      regConfig: apiUrlConfigEntry,

      regConfigDetail: apiUrlConfigEntryDetailTabSearch,

      tableBindColumns: [
        { prop: "id", label: "交易流水号", width: 180 },
        { prop: "workdate", label: "工作日期", type: "date", width: 120 },
        { prop: "pkgId", label: "报文标识号", width: 120 },
        { prop: "dbtMngType", label: "扣款管理类型", dictCode: "DebitManagementTypeCode", width: 120 },
        { prop: "dbtNo", label: "扣款通知编号", width: 120 },
        { prop: "regionId", label: "地域标识", dictCode: "RegionId", width: 120 },
        { prop: "uniformScCode", label: "统一社会信用代码", width: 180 },
        { prop: "payerNum", label: "付款人数目" },
      ],

      tableBindColumnsDetail: [
        { prop: "dbtDtlId", label: "扣款明细标识号", width: 180 },
        { prop: "payerName", label: "付款人名称", width: 120 },
        { prop: "payerAddr", label: "付款人地址", width: 120 },
        { prop: "payerActno", label: "付款人账号", width: 120 },
        { prop: "payerAccBrno", label: "付款人开户行行号", width: 180 },
        { prop: "payerBrno", label: "付款行行号", width: 120 },
        { prop: "curcd", label: "币种", width: 120 },
        { prop: "amount", label: "金额", type: "price", width: 120 },
      ],
      tableBindColumnsDbt: [
        { prop: "dbtDtlId", label: "扣款结果明细标识号" },
        { prop: "dbtStatus", label: "扣款结果业务状态", dictCode: "CHECK_PROC_STATUS" },
      ],

      opButtonList: () => {
        return [
          {
            label: `${this.lang("common.add")}`,
            type: "primary",
            icon: "el-icon-plus",
            clickFun: () => {
              this.add();
            },
          },
        ];
      },

      dictMapAsync: {},
      // 弹出层标题
      title: "",
      titleDtb: "",
      // 是否显示弹出层
      open: false,
      openDtb: false,
      // 表单参数
      detailForm: {index: undefined},
      detailFormDtb: {
        dbtDtlId: undefined,
        dbtStatus: undefined,
      },
      userInfo: {},
      totalRecord: 0,
      totalPage: 0,
      needCount: 0,

      rules: {
        sender: [{ max: 14, message: "最大长度为14位：", trigger: "blur" }],
        senderName: [{ max: 60, message: "最大长度为60位：", trigger: "blur" }],
        recver: [{ max: 14, message: "最大长度为14位：", trigger: "blur" }],
        recverName: [{ max: 60, message: "最大长度为60位：", trigger: "blur" }],
        bizStatus: { required: true, message: "请输入业务状态：", trigger: "blur" },
        orgDbtNo: { required: true, message: "请输入原扣款通知编号：", trigger: "blur" },
        payerNum372: { required: true, message: "请输入付款人数目：", trigger: "blur" },
        bizRejectCode: { required: true, message: "请输入业务拒绝处理码：", trigger: "blur" },
        bizRejectCodeDesc: { required: true, message: "请输入业务拒绝处理码：", trigger: "blur" },
      },
      rulesDtb: {
        dbtDtlId: { required: true, message: "请输入扣款结果明细标识号：", trigger: "blur" },
        dbtStatus: { required: true, message: "请输入扣款结果业务状态：", trigger: "blur" },
      },
    };
  },
  mounted() {
    const formOptions = [
      // 普通搜索

      {
        type: "input",
        prop: "id",
        defaultAttr: {
          size: "small",
          label: "交易流水号",
          placeholder: "请输入交易流水号",
        },
      },
      {
        type: "dates",
        nameObj: {
          startTimeName: "workdate",
          endTimeName: "workdate2",
        },
        defaultAttr: {
          size: "small",
          label: "查询日期",
          placeholder: ["请输入工作开始日期", "请输入工作结束日期"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyyMMdd",
        },
      },
    ];
    this.formOptions = formOptions;
    this.dictMapAsync = this.getDictMap();
  },
  methods: {
    /***
     * 按钮操作汇总
     */
    /***
     * 按钮操作汇总
     */
    handleAction(type, row = {}) {
      switch (type) {
        case "detail":
          this.handleDetail(row);
          break;
      }
    },

    // 关闭按钮
    cancel() {
      this.open = false;
    },

    // 关闭按钮
    cancelDtb() {
      this.resetForm("detailFormDtb");
      this.openDtb = false;
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      this.title = "客户账户扣款管理应答录入明细";
      custAccDeductionAnswerEntryDetail(row).then((response) => {
        this.detailForm = response.result.data;
        this.userInfo = response.result.userInfo;
        if (this.detailForm.indirectSender !== null) {
          this.$set(this.detailForm, "recver", this.detailForm.indirectSender);
          this.$set(this.detailForm, "recverName", this.detailForm.indirectSenderName);
        } else {
          this.$set(this.detailForm, "recver", this.detailForm.directSender);
          this.$set(this.detailForm, "recverName", this.detailForm.directSenderName);
        }
        this.$set(this.detailForm, "oriPkgId", this.detailForm.pkgId);
        this.$set(this.detailForm, "oriSender", this.detailForm.directSender);
        this.$set(this.detailForm, "oriMsgType", "beps.371.001.01");
        this.$set(this.detailForm, "payerNum372", 0);
        this.queryParamsDetail.id = this.detailForm.id;
        this.open = true;
        this.$nextTick(() => {
          this.$refs.dataTableDetail.doQuery();
        });
        // 应答明细信息表格初始化
        this.$refs.dataTableDbt.dataList = [];
      });
    },
    //点击提交按钮
    save: function () {
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          this.detailForm.tableList = this.$refs.dataTableDbt.dataList;
          custAccDeductionAnswerEntrySave(this.detailForm)
            .then((res) => {
              this.loading = true;
              this.msgSuccess("提交成功");
              this.open = false;
              this.loading = false;
              this.$alert(`CORE_ID: ${res.result.rspId}`, "生成交易流水号", {
                confirmButtonText: "确定",
                callback: (action) => {
                },
              });
              this.$nextTick(() => {
                this.$refs.dataTable.doQuery(true);
              });
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },

    openBasedataDialog(dataType = "") {
      this.$refs.basedataDialog.show(dataType);
    },

    confirmData(fromFlag, row) {
      this.$set(this.detailForm, "bizRejectCode", row.dataValue);
      this.$set(this.detailForm, "bizRejectCodeDesc", row.dataDesc);
    },
    //回车获取basedata值
    getbase(dataType) {
      const dataValue = this.detailForm.bizRejectCode;
      getBasedata(dataType, dataValue)
        .then((response) => {
          if (response.code == "S" && response.result !== null && response.result !== undefined) {
            this.$set(this.detailForm, "bizRejectCode", response.result.dataValue);
            this.$set(this.detailForm, "bizRejectCodeDesc", response.result.dataDesc);
          } else {
            this.msgSuccess("找不到对应的字典项");
            this.$set(this.detailForm, "bizRejectCode", null);
            this.$set(this.detailForm, "bizRejectCodeDesc", null);
          }
        })
        .catch((err) => {
          this.$set(this.detailForm, "bizRejectCode", null);
          this.$set(this.detailForm, "bizRejectCodeDesc", null);
        });
    },

    add() {
      this.openDtb = true;
      this.resetForm("detailFormDtb");
      this.$set(this, "detailFormDtb", {});
      this.type = "add";
      this.titleDtb = "扣款结果明细新增";
    },

    // 删除行
    deleteRow(index, row) {
      this.$confirm("确定删除该条信息吗？")
        .then((_) => {
          this.$refs.dataTableDbt.dataList.splice(index, 1);
          this.$set(this.detailForm, "payerNum372", this.$refs.dataTableDbt.dataList.length);
        })
        .catch((_) => {});
    },
    // 维护行
    editRow(index, row) {
      this.openDtb = true;
      this.detailFormDtb = {...row}
      this.$set(this.detailForm,"index",index);
      this.titleDtb = "扣款结果明细维护";
      this.type = "edit"
    },

    detailSave(index) {
      if (this.type === "edit") {
        this.$refs["detailFormDtb"].validate((valid) => {
          if (valid) {
            this.$refs.dataTableDbt.dataList[index].dbtDtlId = this.detailFormDtb.dbtDtlId;
            this.$refs.dataTableDbt.dataList[index].dbtStatus = this.detailFormDtb.dbtStatus;
            this.$refs.dataTableDbt.initColPer(this.$refs.dataTableDbt.dataList);
            this.$set(this.detailForm, "payerNum372", this.$refs.dataTableDbt.dataList.length);
            this.openDtb = false;
          }
        });
      } else {
        this.$refs["detailFormDtb"].validate((valid) => {
          if (valid) {
            this.$refs.dataTableDbt.dataList.push(this.detailFormDtb);
            this.$set(this.detailForm, "payerNum372", this.$refs.dataTableDbt.dataList.length);
            this.openDtb = false;
          }
        });
      }
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
